package controllers.cell;


import entity.Commodity;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import services.SystemContext;

import java.io.IOException;
import java.net.URL;
import java.util.function.UnaryOperator;

public class CommodityCellRenderer implements Callback<ListView<Commodity>, ListCell<Commodity>> {
    @Override
    public ListCell<Commodity> call(ListView<Commodity> param) {
        ListCell<Commodity>cell=new ListCell<Commodity>(){

            @Override
            protected void updateItem(Commodity item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(null);
                setText(null);
                if(item!=null) {
                    FXMLLoader fx = new FXMLLoader();
                    URL resource = getClass().getClassLoader().getResource("views/CommodityCell.fxml");
                    if (resource == null)
                        throw new RuntimeException("资源文件找不到");
                    fx.setLocation(resource);
                    AnchorPane an = null;
                    try {
                        an = (AnchorPane) fx.load();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ImageView imageView = (ImageView) an.lookup("#picture");
                    Image image = new Image("File:" + item.getPicture(), 148, 148, false, true, true);
                    imageView.setImage(image);

                    TextField NameLabel = (TextField) an.lookup("#nameFd");
                    NameLabel.setText(item.getName());

                    TextArea commitLabel = (TextArea) an.lookup("#commitTA");
                    commitLabel.setText(item.getDescribe());
                    TextField priceFd = (TextField) an.lookup("#priceFd");
                    priceFd.setText(item.getPrice().toString());

                    TextField surplus = (TextField) an.lookup("#surplus");
                    surplus.setText(item.getQuantity().toString());
                    setOnlyNumber(surplus);

                    Label label=(Label) an.lookup("#id");
                    System.out.println(item.getCommodity_id().toString());
                    label.setText(item.getCommodity_id().toString());

                    if(SystemContext.getInstance().getIsCustomer()){
                        an.lookup("#updateBtn").setVisible(false);
                        an.lookup("#deleteBtn").setVisible(false);
                    }else{
                        an.lookup("#addBtn").setVisible(false);
                        an.lookup("#numberField").setVisible(false);
                    }
                    setGraphic(an);
                }
            }
        };
        return cell;
    }


    public void setOnlyNumber(TextField textField){
        textField.setTextFormatter(new TextFormatter<String>(new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change change) {
                String value = change.getText();
                if(value.matches("[0-9]*")){
                    return change;
                }
                return null;
            }
        }));
    }

}
